package com.x.Servlet_02_ServletContext;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * ClassName: ServletContextTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.08.06 21:56
 */

//@WebServlet("/ServletContext")
public class ServletContextTest extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("************ServletContext文件api*************");
        ServletContext servletContext = super.getServletContext();

        //获得一个项目部署位置下某个文件的磁盘真实路径
        String path = servletContext.getRealPath("upload");
        System.out.println("项目部署位置下文件的磁盘真实路径：" + path);


        System.out.println("************ServletContext项目路径api*************");

        String contextPath = servletContext.getContextPath();
        System.out.println("项目的上下文路径（浏览器访问路径）：" + contextPath);


        System.out.println("************ServletContext域对象api*************");

        servletContext.setAttribute("ka","va");
        servletContext.setAttribute("ka","vaa");

        String  ka = (String) servletContext.getAttribute("ka");
        System.out.println(ka);
        servletContext.removeAttribute("ka");
    }
}
